﻿using Utils;

namespace Red.Controls.Diagrams.ViewModels
{
    /// <summary>
    /// Defines a network of nodes and connections between the nodes.
    /// </summary>
    public sealed class NetworkViewModel
    {
        #region Data

        /// <summary>
        /// The collection of nodes in the network.
        /// </summary>
        private ImpObservableCollection<NodeViewModel> _nodes;

        /// <summary>
        /// The collection of connections in the network.
        /// </summary>
        private ImpObservableCollection<ConnectionViewModel> _connections;

        #endregion Data

        #region Properties

        /// <summary>
        /// The collection of nodes in the network.
        /// </summary>
        public ImpObservableCollection<NodeViewModel> Nodes
        {
            get
            {
                if ( _nodes == null )
                {
                    _nodes = new ImpObservableCollection<NodeViewModel>();
                }

                return _nodes;
            }
        }

        /// <summary>
        /// The collection of connections in the network.
        /// </summary>
        public ImpObservableCollection<ConnectionViewModel> Connections
        {
            get
            {
                if ( _connections == null )
                {
                    _connections = new ImpObservableCollection<ConnectionViewModel>();
                    _connections.ItemsRemoved += connections_ItemsRemoved;
                }

                return _connections;
            }
        }

        #endregion Properties

        #region Event handlers

        /// <summary>
        /// Event raised then Connections have been removed.
        /// </summary>
        private void connections_ItemsRemoved( object sender, CollectionItemsChangedEventArgs e )
        {
            foreach ( ConnectionViewModel connection in e.Items )
            {
                connection.SourceConnector = null;
                connection.DestConnector = null;
            }
        }

        #endregion Event handlers
    }
}
